package com.smasher.widget.scan;

import android.view.KeyEvent;

import java.util.HashSet;
import java.util.Set;

/**
 * Builder类用于构建ScanKeyManager实例
 */
public class ScanManagerBuilder {
    private OnScanValueListener listener;
    private Set<Integer> endKeys = new HashSet<>();

    public ScanManagerBuilder() {
        // 默认添加Enter键作为结束符
        endKeys.add(KeyEvent.KEYCODE_ENTER);
    }

    /**
     * 设置扫描结果监听器
     */
    public ScanManagerBuilder setListener(OnScanValueListener listener) {
        this.listener = listener;
        return this;
    }

    /**
     * 添加扫描结束符
     */
    public ScanManagerBuilder addEndKey(int keyCode) {
        this.endKeys.add(keyCode);
        return this;
    }

    /**
     * 批量添加扫描结束符
     */
    public ScanManagerBuilder addEndKeys(int... keyCodes) {
        for (int keyCode : keyCodes) {
            this.endKeys.add(keyCode);
        }
        return this;
    }

    /**
     * 设置自定义结束符集合，会替换默认的结束符
     */
    public ScanManagerBuilder setEndKeys(Set<Integer> endKeys) {
        if (endKeys != null && !endKeys.isEmpty()) {
            this.endKeys = new HashSet<>(endKeys);
        }
        return this;
    }

    /**
     * 清除所有结束符（谨慎使用，需要手动添加结束符）
     */
    public ScanManagerBuilder clearEndKeys() {
        this.endKeys.clear();
        return this;
    }

    /**
     * 获取扫描结果监听器
     */
    public OnScanValueListener getListener() {
        return listener;
    }

    /**
     * 获取自定义结束符集合
     */
    public Set<Integer> getEndKeys() {
        return endKeys;
    }

    /**
     * 构建ScanKeyManager实例
     */
    public ScanKeyManager build() {
        if (endKeys.isEmpty()) {
            // 确保至少有一个结束符
            endKeys.add(KeyEvent.KEYCODE_ENTER);
        }
        return new ScanKeyManager(this);
    }
}